/**
 * Cell.m
 * Cocoa Tetris
 *
 * Copyright 2009 Wei Yeh, Evelyn Yung. All rights reserved.
 */

#import "TetrisController.h"

@implementation Cell

-(id)initWithX:(unsigned int)x y:(unsigned int)y
{
	if (![super init]) return nil;
		
	myX = x;
	myY = y;
	myRect = NSMakeRect(myX * [Tetromino size], myY * [Tetromino size], [Tetromino size], [Tetromino size]);
	myTetromino = nil;
	
	return self;
}

-(bool)markWithTetromino:(Tetromino *)tetromino
{
	if ([self isOccupied]) return NO;
	myTetromino = tetromino;
	return YES;
}

-(void)unmark
{
	myTetromino = nil;
}

-(bool)isOccupied
{
	return myTetromino != nil;
}

-(void)display 
{
	NSColor *color = [self isOccupied] ? [myTetromino color] : [NSColor whiteColor];
	[color set];
	NSRectFill(myRect);
	if ([self isOccupied]) {
		NSImage *image = [NSImage imageNamed:@"block_bg.png"];
		
		[image drawInRect:myRect
				 fromRect:NSZeroRect
				operation:NSCompositeSourceOver
				 fraction:1.0];
	}
}

-(NSRect)rect 
{
	return myRect;
}

-(unsigned int)y 
{
	return myY;
}

-(unsigned int)x 
{
	return myX;
}

-(Tetromino *)tetromino
{
	return myTetromino;
}

@end
